package com.wise.service.cms.content.applicationarticles;

import com.wise.entities.content.ApplicationArticle;
import com.wise.entities.content.ApplicationColumn;
import com.wise.framework.dao.SqlSelect;
import com.wise.framework.daomodel.DaoOne;
import com.wise.framework.daomodel.DaoPageList;
import com.wise.framework.restful.PageResultList;
import com.wise.summer.v1.cms.content.applicationarticles.ListApplicationArticles;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import static com.wise.model.content.ContentErrMsgs.Err_001600250014;

/**
 * @author hejiawei
 * @date 2020/10/27 12:40
 */
@Service
@SummerService
public class ListApplicationArticlesService implements SummerServiceBean<ListApplicationArticles> {
    @Override
    public void sum(ListApplicationArticles model) {
        SqlSelect sql = SqlSelect.selec("*")
                .fromClassName(ApplicationArticle.class)
                .like(!StringUtils.isEmpty(model.getTitle()), ApplicationArticle._title, "%" + model.getTitle() + "%")
                .eq(!StringUtils.isEmpty(model.getColumnId()), ApplicationArticle._columnId, model.getColumnId())
                .eq(model.getState() != null, ApplicationArticle._state, model.getState())
                .eqLang(ApplicationArticle._lang)
                .order_by("id desc")
                .order_by(ApplicationArticle._sort);

        PageResultList<ApplicationArticle> pageResultList = DaoPageList.s(ApplicationArticle.class, sql, model.getStart(), model.getLimit());

        pageResultList.getList().forEach(article -> {
            String columnName = DaoOne.s(ApplicationColumn.class, article.getColumnId(), Err_001600250014).getColumnName();
            article.setColumnName(columnName);
        });

        model.setSummerResult(pageResultList);
    }
}
